import { createRouter, createWebHistory } from 'vue-router'

// 页面组件
import HomePage from '../pages/home/index.vue'
import BlogPage from '../pages/blog/index.vue'
import BlogDetailPage from '../pages/subpages/blog/detail.vue'
import MessagePage from '../pages/message/index.vue'
import ProfilePage from '../pages/profile/index.vue'
import WeatherPage from '../pages/weather/index.vue'
import WeatherDetailPage from '../pages/weather/detail.vue'
import ArticleCreatePage from '../pages/article/create.vue'
import AIChatPage from '../pages/subpages/ai/index.vue'

// 创建路由实例
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: HomePage,
      meta: { title: '首页' }
    },
    {
      path: '/blog',
      name: 'Blog',
      component: BlogPage,
      meta: { title: '博客' }
    },
    {
      path: '/blog/:id',
      name: 'BlogDetail',
      component: BlogDetailPage,
      meta: { title: '博客详情' }
    },
    {
      path: '/message',
      name: 'Message',
      component: MessagePage,
      meta: { title: '消息' }
    },
    {
      path: '/profile',
      name: 'Profile',
      component: ProfilePage,
      meta: { title: '我的' }
    },
    {
      path: '/weather',
      name: 'Weather',
      component: WeatherPage,
      meta: { title: '天气' }
    },
    {
      path: '/weather/:city',
      name: 'WeatherDetail',
      component: WeatherDetailPage,
      meta: { title: '天气详情' }
    },
    {
      path: '/article/create',
      name: 'ArticleCreate',
      component: ArticleCreatePage,
      meta: { title: '创作文章' }
    },
    {
      path: '/article/edit/:id',
      name: 'ArticleEdit',
      component: ArticleCreatePage,
      meta: { title: '编辑文章' }
    },
    {
      path: '/ai-chat',
      name: 'AIChat',
      component: AIChatPage,
      meta: { title: 'AI聊天' }
    },
    {
      path: '/bookmark',
      name: 'Bookmark',
      component: () => import('../pages/subpages/bookmark/index.vue'),
      meta: { title: '收藏夹' }
    },
    {
      path: '/scan',
      name: 'Scan',
      component: () => import('../pages/subpages/scan/index.vue'),
      meta: { title: '扫一扫' }
    },
    {
      path: '/compiler',
      name: 'Compiler',
      component: () => import('../pages/subpages/compiler/index.vue'),
      meta: { title: '在线编译器' }
    },
    // 添加我的创作、点赞和浏览历史页面的路由
    {
      path: '/profile/articles',
      name: 'MyArticles',
      component: () => import('../pages/subpages/profile/articles/index.vue'),
      meta: { title: '我的创作' }
    },
    {
      path: '/profile/likes',
      name: 'MyLikes',
      component: () => import('../pages/subpages/profile/likes/index.vue'),
      meta: { title: '我的点赞' }
    },
    {
      path: '/profile/history',
      name: 'MyHistory',
      component: () => import('../pages/subpages/profile/history/index.vue'),
      meta: { title: '浏览历史' }
    },
    {
      path: '/exercise',
      name: 'ExerciseList',
      component: () => import('@/pages/subpages/exercise/ExerciseList.vue')
    },
    {
      path: '/exercise/:id',
      name: 'ExerciseDetail',
      component: () => import('@/pages/subpages/exercise/ExerciseDetail.vue')
    },
    // 404页面
    {
      path: '/:pathMatch(.*)*',
      redirect: '/'
    }
  ]
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - Vue3应用` : 'Vue3应用'
  next()
})

export default router